package earogya.ehr.patient_details.ws;

import java.io.IOException;

import org.hibernate.HibernateException;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v251.segment.*;
import ca.uhn.hl7v2.model.v251.datatype.XAD;
import ca.uhn.hl7v2.model.v251.datatype.XPN;
import ca.uhn.hl7v2.model.v251.message.*;
import ca.uhn.hl7v2.parser.EncodingNotSupportedException;
import ca.uhn.hl7v2.parser.GenericParser;
import ca.uhn.hl7v2.parser.Parser;
import ca.uhn.hl7v2.parser.PipeParser;
import earogya.ehr.patient_details.messages.*;

public class HL7MessageHandler {

	public String parser(String message){
		//Parser p = new GenericParser();
		PipeParser p=new PipeParser();
		String ack=null;
		 Message hapiMsg;
		 try {
		 // The parse method performs the actual parsing
			 hapiMsg = p.parse(message);
		 	 MSH mshSegment=(MSH)hapiMsg.get("MSH");

		 	 String msgType=mshSegment.getMessageType().getMessageCode().getValue();

			 String msgTrigger = mshSegment.getMessageType().getTriggerEvent().getValue();
			 ack=identifyMessage(msgType, msgTrigger, hapiMsg);
			 System.out.println(msgType+" "+msgTrigger);
		 } catch (EncodingNotSupportedException e) {
			 e.printStackTrace();
		 } catch (HL7Exception e) {
			 e.printStackTrace();
		 } catch (HibernateException e) {
			 e.printStackTrace();
		} catch (IOException e) {
			 e.printStackTrace(); 
		}	
		 return ack;
	}
	
	
	private String identifyMessage(String messageCode, String triggerEvent, Message hapiMsg) throws HibernateException, IOException, HL7Exception{
		String ack=null;
		if(messageCode.equals("ADT")){
			ack=this.identifyTriggerEventForADT(triggerEvent, hapiMsg);
		}else if(messageCode.equals("ADR")){
			this.identifyTriggerEventForADR(triggerEvent);
		}else if(messageCode.equals("ACK")){
			this.identifyTriggerEventForACK(triggerEvent);
		}else if(messageCode.equals("ORU")){
			this.identifyTriggerEventForORU(triggerEvent);
		}else if(messageCode.equals("QRY")){
			ack=this.identifyTriggerEventForQRY(triggerEvent,hapiMsg);
		}else{
			System.out.println("Unexpected trigger event");
		}
		return ack;
	}
	
	private String identifyTriggerEventForADT(String triggerEvent, Message hapiMsg) throws HibernateException, IOException, HL7Exception{
		String ack=null;
		if(triggerEvent.equals("A04")){
			 ADT_A01 adtMsg = (ADT_A01)hapiMsg;
			 ADT_A04_Handler handler=new ADT_A04_Handler(adtMsg);
			 ack=handler.manageMessage();
		}
		else if(triggerEvent.equals("A08")){
			ADT_A01 adtMsg = (ADT_A01)hapiMsg;
			ADT_A08_Handler handler=new ADT_A08_Handler(adtMsg);
			ack=handler.manageMessage();
		}
		
		else if(triggerEvent.equals("A60")){
			ADT_A60 adtMsg = (ADT_A60)hapiMsg;
			ADT_A60_Handler handler=new ADT_A60_Handler(adtMsg);
			ack=handler.manageMessage();
		}
		else{
			System.out.println("Unexpected message");
		}
		return ack;
	}
	
	private void identifyTriggerEventForACK(String triggerEvent){
		
	}
	
	private void identifyTriggerEventForADR(String triggerEvent){
		
	}
	
	private void identifyTriggerEventForORU(String triggerEvent){
		
	}
	
	private String identifyTriggerEventForQRY(String triggerEvent,Message hapiMsg) throws HibernateException, IOException, HL7Exception{
		String ack=null;
		if(triggerEvent.equals("A19")){
			 QRY_A19 qryMsg = (QRY_A19)hapiMsg;
			 QRY_A19_Handler handler=new QRY_A19_Handler(qryMsg);
			 ack=handler.manageMessage();
		}
		return ack;
	}
	
	
	public static void main(String[] args) throws HL7Exception, IOException{
		String msg = "MSH|^~\\&|HIS|RIH|EKG|EKG|199904140038||ADT^A01||P|2.5.1\r"
			// +"PID|0001|00009874|00001122|A00977|SMITH^JOHN^M|MOM|19581119|F|NOTREAL^LINDA^M|C|564 SPRING ST^^NEEDHAM^MA^02494^US|0002|(818)565-1551|(425)828-3344|E|S|C|0000444444|252-00-4414||||SA|||SA||||NONE|V1|0001||D.ER^50A^M110^01|ER|P00055|11B^M011^02|070615^BATMAN^GEORGE^L|555888^NOTREAL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^NOTREAL^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|199904101200||||5555112333|||666097^NOTREAL^MANNY^P\r"
			 +"NK1|1|SAMPLE^ELLEN|SPOUSE^^HL70063|171 SAMPLELIN^^ISHSAMPLE^MI^49849^\"\"^|(900)485-5344|(900)545-1234~(900)545-1200|EC1^FIRST EMERGENCY CONTACT^HL70131\r";
		 //	+ "IN1||022254P|4558PD|BLUE CROSS|STREET^OTHER STREET^CITY^ST^00990||(333)333-6666||221K|LENIX|||19980515|19990515|||PATIENT01 TEST D||||||||||||||||||02LL|022LP554";
		/*String msg="MSH|^~\\&|REGADT|MCM|IFENG||199112311501||ADT^A04^ADT_A01|000001|P|2.5||| EVN|A04|199901101500|199901101400|01||199901101410 PID|||191919^^SAMPLE^MR~371-66-9256^^^USSSA^SS|253763|SAMPLE^JAMES^A||19560129|M|||999 SAMPLELIN^^ISHSAMPLE^MI^49849^\"\"^||800)485-5344|(888)485-5344||S^^HL70002|C^^HL70006|10199925^^^SAMPLE^AN|371-66-9256|| NK1|1|SAMPLE^ELLEN|SPOUSE^^HL70063|171 SAMPLELIN^^ISHSAMPLE^MI^49849^\"\"^|(900)485-5344|(900)545-1234~(900)545-1200|EC1^FIRST EMERGENCY CONTACT^HL70131 NK1|2|SAMPLE^MARYLOU|MOTHER^^HL70063|300 SAMPLELIN^^ISHSAMPLE^MI^49849^\"\"^|(900)485-5344|(900)545-1234~(900)545-1200|EC2^SECOND EMERGENCY CONTACT^HL70131 NK1|3 NK1|4|||123 SAMPLERY WAY^^ISHSAMPLE^MI^49849^\"\"^||(800)545-1200|EM^EMPLOYER^HL70131|19940605||PROGRAMMER|||SAMPLE SOFTWARE COMPANY PV1||O|O/R||||0148^SAMPLEN,JAMES|0148^SAMPLEN,JAMES||AMB|||||||0148^SAMPLEN,JAMES|S|1400|A|||||||||||||||||||SAMPLE|||||199501101410| PV2||||||||199901101400|||||||||||||||||||||||||199901101400 ROL||AD|CP^^HL70443|0148^SAMPLEN,JAMES OBX||NM|3141-9^BODY WEIGHT^LN||62|kg|||||F OBX||NM|3137-7^HEIGHT^LN||190|cm|||||F DG1|1|19|R63.4^LOSS OF WEIGHT^I10|||00| GT1|1||SAMPLE^JAMES^\"\"^\"\"^\"\"^\"\"^||171 SAMPLELIN^^ISHSAMPLE^MI^49849^\"\"^|(800)485-5344|            (800)485-5344      ||||SE^SELF^HL70063|371-66-925||||SAMPLE AUTO PLACE|171 SAMPLELIN^^ISHSAMPLE^MI^49849^\"\"|(800)485-5344| IN1|0|0^HL70072|BC1|BLUE CROSS|171 SAMPLELIN^^ISHSAMPLE^M149849^\"\"^||(900)485-5344|90||||||50 OK| ";*/
		
		String msgA04="MSH|^~\\&|GUI|CSTH|EHR|CSTH|20110603||ADT^A04||P|2.5.1\r"
			+"EVN|A04|20030822172800|||1\r"
			+"PID|||866461694V||Wijekoon^Ureka Sanjeewani^^Ms||19860525223451|F||Sinhalese|&&234^Moratuwa^Colombo^^^Sri Lanka||(+94)772515092^^^ureka@gmail.com|||S|3|||||5|Kurunegala|0||||||0|\r"
			+"PD1||2||||||3|\r"
			+"DB1||||4|19970623|19990622|\r"
			+"AL1|||AA|N||20091011|";
		
		String msgA08="MSH|^~\\&|GUI|CSTH|EHR|CSTH|20110603||ADT^A08||P|2.5.1\r"
			+"EVN|A08|20030822172800|||1\r"
			+"PID||56|00001122V||||||||^Katubedda^^^^|||||||\r"
			+"PD1||||||||5|\r"
			+"DB1|||SS|D||19990622|";
		
		String msgA60="MSH|^~\\&|HNAM|CAPIOUK|JDE|JDE|20030821153359||ADT^A60||P|2.5.1\r"
			+"EVN|A60|20030822172800|||1\r"
			+"PID|1|00009874|00001122V|A00977|SMITH^JOHN^M|MOM|19581119||NOTREAL^LINDA^M|C|564 SPRING ST^^NEEDHAM^MA^02494^US|0002|(818)565-1551|(425)828-3344|E|S|3|0000444444|252-00-4414||||SA|||SA||||NONE|V1|0001||D.ER^50A^M110^01|ER|P00055|11B^M011^02|070615^BATMAN^GEORGE^L|555888^NOTREAL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^NOTREAL^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|199904101200||||5555112333|||666097^NOTREAL^MANNY^P\r"
			+"IAM|||AAAA|DD|||||||||20110524124533|";
		
		String msgQRYA19="MSH|^~\\&|KIS||CommServer||200811111017||QRY^A19||P|2.5.1|\r"
			+"QRD|200811111016|R|I|Q1004|||1^RD|866461694V|DEM|||";

		HL7MessageHandler h=new HL7MessageHandler();
		String ack=h.parser(msgQRYA19);
		System.out.println(ack);
		
	}
}
